<?php

namespace search\controllers\bigdata;

use business\elasticsearch\services\BigDataSaasSupplierService;

class SaasSupplierController extends BaseController
{
    public $enableCsrfValidation = false;//csrf安全

    /**
     * @api {get} /bigdata/saas-supplier/get-list 01、大数据在建项目搜索
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataSaasSupplier
     *
     * @apiDescription 01、大数据工程单位
     *
     * @apiParam {String} keyword                                            搜索词
     * @apiParam {String} province                                           省份
     * @apiParam {String} city                                               城市
     * @apiParam {String} page                                               页码
     * @apiParam {String} pageSize                                           分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     *
     */
    public function actionGetList()
    {
        $keyword = $this->getParam('keyword');
        $province = $this->getParam('province_code');
        $city = $this->getParam('city_code');
        $areaStr = $this->getParam('area');
        $projectProvince = $this->getParam('project_province_code');
        $projectCity = $this->getParam('project_city_code');
        $projectAreaStr = $this->getParam('project_area');
        $projectStartTime = $this->getParam('project_start_time');
        $projectEndTime = $this->getParam('project_end_time');
        $industryCategoryMiddle = $this->getParam('industry_category_middle');
        $createdTime = $this->getParam('created_time');
        $page = $this->getParam('page');
        $pageSize = $this->getParam('page_size');
        $page = intval($page) > 0 ? intval($page) : 1;
        $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;
        if (!empty($province)) {
            $province = explode(",", $province);
        } else {
            $province = [];
        }
        $area = array_filter(explode(",", $areaStr));
        $projectArea = array_filter(explode(",", $projectAreaStr));
        $keyword = array_filter(explode(",", $keyword));
        $projectArea = array_filter(array_unique(array_merge($projectArea, [$projectProvince], [$projectCity])));
        $industryCategoryMiddle = array_filter(explode(",", $industryCategoryMiddle));

        try {
            $data = (new BigDataSaasSupplierService())->getDataList(
                $keyword,
                $province,
                $city,
                $area,
                $projectArea,
                $projectStartTime,
                $projectEndTime,
                $industryCategoryMiddle,
                $createdTime,
                $page,
                $pageSize
            );

            return $this->success($data);
        } catch (\Exception $exception) {
            var_dump($exception->getTrace());die;
            \Yii::error('找工程单位搜索失败:' . $exception->getMessage());
            return $this->error(-1, $exception->getMessage());
        }
    }


    /**
     * @api {get} /bigdata/saas-supplier/guess-names 02、工程单位联想
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataSaasSupplier
     *
     * @apiDescription 02、工程单位联想
     *
     * @apiParam {String} keyword                   搜索词
     * @apiParam {String} pageSize                  分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     * @apiSuccess {Number}  data.list.titleHL     搜索词高亮开工项目名
     * @apiSuccess {String}  data.list.title       开工项目
     *
     *
     */
    public function actionGuessNames()
    {
        try {
            $keyword = $this->getParam('keyword');
            $pageSize = $this->getParam('page_size');
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            if (empty($keyword)) {
                throw new \Exception('你的关键字没有传');
            }
            $bigDataSaasSupplierService = new BigDataSaasSupplierService();
            $rs = $bigDataSaasSupplierService->guessNames($keyword, $pageSize);
            return $this->success($rs);

        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }
}